package com.azzcs.service;

import com.azzcs.coyote.connector.Connector;
import com.azzcs.core.ThreadExecutor;
import com.azzcs.catalina.engine.Engine;
import com.azzcs.lifecycle.LifecycleBase;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * @Author: wzg
 * @Date: 2021/1/7 下午4:16
 */
public class StandardService extends LifecycleBase implements Service {
    private String name;
    private Engine engine;
    protected List<Connector> connectors = new ArrayList<>();
    protected final ArrayList<ThreadExecutor> executors = new ArrayList<>();
    @Override
    protected void initInternal() {

    }

    @Override
    protected void startInternal() {
        for (Connector connector : connectors) {
            connector.start();
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public List<Connector> getConnectors() {
        return connectors;
    }

    public void setConnectors(List<Connector> connectors) {
        this.connectors = connectors;
    }

    public ArrayList<ThreadExecutor> getExecutors() {
        return executors;
    }

    public void addThreadExecutor(ThreadExecutor threadExecutor) {
        this.executors.add(threadExecutor);
    }

    public void addConnector(Connector connector) {
        this.connectors.add(connector);
    }

    @Override
    public Executor getExecutor(String name) {
        for (ThreadExecutor executor : executors) {
            if(executor.getName().equalsIgnoreCase(name)) {
                return executor;
            }
        }
        return null;
    }
}
